#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>

 
struct ListNode {
    int val;
    struct ListNode *next;
};



struct ListNode* removeNthFromEnd(struct ListNode* head, int n) {
    if (head == NULL || head->next == NULL)
        return NULL;
    struct ListNode* newhead = (struct ListNode*)malloc(sizeof(struct ListNode));
    newhead->next = head;
    struct ListNode* slow = newhead;
    struct ListNode* fast = newhead;
    while (n--)
    {
        fast = fast->next;
    }
    struct ListNode* cur = newhead;
    while (fast)
    {
        cur = slow;
        fast = fast->next;
        slow = slow->next;
    }
    cur->next = slow->next;
    struct ListNode* ret = newhead->next;
    free(slow);
    free(newhead);

    return head;
}
int main() {
    struct ListNode* l1= (struct ListNode*)malloc(sizeof(struct ListNode));
    struct ListNode* l2 = (struct ListNode*)malloc(sizeof(struct ListNode));
    l1->next = l2;
    l2->next = NULL;
    l1->val = 1;
    l2->val = 2;
    removeNthFromEnd(l1, 2);
    return 0;
}